import AntModal from '@/components/pt/dialog/AntModal'
import SelectPost from '@/views/platform/components/SelectPost'
import SelectComment from '@/views/platform/components/SelectComment'
import SelectUser from '@/components/pt/selectUser/SelectUser'
import { getComment, addComment, updateComment } from '@/api/platform/comment'
import { getUser, getUserProfile } from '@/api/system/user'

export default {
  name: 'CreateForm',
  props: {
    postId: {
      type: String,
      required: false
    },
    statusOptions: {
      type: Array,
      required: true
    }
  },
  components: {
    AntModal,
    SelectUser,
    SelectPost,
    SelectComment
  },
  data() {
    return {
      open: false,
      closeDialog: true,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      disabled: false,
      chooseUserDisabled: true,
      choosePostDisabled: false,
      total: 0,
      id: undefined,
      formTitle: '添加评论管理',
      // 表单参数
      form: {},
      rules: {
        postId: [{ required: true, message: '请选择帖子', trigger: 'blur' }],
        userId: [{ required: true, message: '请选择用户', trigger: 'blur' }],
        content: [{ required: true, message: '评论内容不能为空', trigger: 'blur' }]
      }
    }
  },
  filters: {},
  created() {},
  computed: {},
  watch: {
    'form.postId': {
      handler(newValue, oldValue) {
        if (oldValue) {
          if (newValue !== oldValue) {
            this.form.pid = undefined
          }
        }
      }
    },
    'form.pid': {
      handler(newValue, oldValue) {
        if (newValue) {
          this.form.postId = this.form.pid.postId
        }
      }
    }
  },
  mounted() {},
  methods: {
    roleControl() {
      if (!this.$store.state.user.roles.includes('admin')) {
        this.chooseUserDisabled = false
        this.choosePostDisabled = true
        return true
      }
      return false
    },
    onClose() {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        pid: undefined,
        postId: this.postId,
        userId: undefined,
        content: undefined,
        isChild: '0',
        replyNumber: 0,
        likeNumber: 0,
        reportNumber: 0,
        status: '0'
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.formTitle = '添加评论'
      if (this.roleControl()) {
        getUserProfile().then(response => {
          this.form.userId = { ids: response.data.user.id, names: response.data.user.name }
        })
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.open = true
      this.spinning = !this.spinning
      const commentId = row.id
      getComment(commentId).then(response => {
        this.form = response.data
        if (!this.roleControl()) {
          if (this.form.userId !== null) {
            getUser(this.form.userId).then(res => {
              this.form.userId = { ids: this.form.userId, names: res.data.name }
            })
          }
        }
        if (this.form.pid !== null) {
          this.form.pid = { postId: this.form.postId, commentId: this.form.pid }
        }
        this.formTitle = '修改评论管理'
        this.spinning = !this.spinning
      })
    },
    /** 提交按钮 */
    submitForm: function (closeDialog) {
      this.closeDialog = closeDialog
      this.disabled = true
      this.$refs.form.validate(valid => {
        if (valid) {
          const saveForm = JSON.parse(JSON.stringify(this.form))
          if (this.form.userId !== null && this.form.userId !== undefined) {
            saveForm.userId = this.form.userId.ids
          }
          if (this.form.pid !== null && this.form.pid !== undefined) {
            saveForm.pid = this.form.pid.commentId
          }
          if (this.form.id !== undefined) {
            updateComment(saveForm)
              .then(response => {
                this.$message.success('更新成功', 3)
                this.open = false
                this.$emit('ok')
                this.$emit('close')
                this.disabled = false
              })
              .catch(error => {
                this.disabled = false
              })
          } else {
            addComment(saveForm)
              .then(response => {
                this.$message.success('新增成功', 3)
                this.open = false
                this.$emit('ok')
                this.$emit('close')
                this.disabled = false
              })
              .catch(error => {
                this.disabled = false
              })
          }
        } else {
          this.disabled = false
          return false
        }
      })
    },
    back() {
      const index = '/platform/comment/index'
      this.$router.push(index)
    }
  }
}
